"use strict";

/**
 * @ngdoc function
 * @name jgappApp.controller:TaskreviewCtrl
 * @description
 * # TaskreviewCtrl
 * Controller of the jgappApp
 */
angular
  .module("jgappApp")
  .controller(
    "TaskreviewCtrl",
    function (
      $scope,
      $timeout,
      $routeParams,
      localStorageService,
      $location,
      $rootScope,
      FileService,
      TaskReviewService,
      DataService,
      Util,
      openApi,
      submitAudit,
      OpenFile
    ) {
      // 详情接口，审批记录接口，提交审核接口
      var id = ($scope.process_id = $routeParams.id);
      $scope.taskId = id;
      // 审批操作
      var aduitAction;
      var detailDataUrl, submiturl;
      var TVtype;
      TVtype = Util.getTVType();
      switch (TVtype) {
        case Util.CHECK: //待审批
          detailDataUrl = openApi.allDetailApi;
          break;
        case Util.VIEW: //待审批
          detailDataUrl = openApi.allDetailApi;
          break;
        case Util.READ: //无需审批
          detailDataUrl = openApi.getTaskInfo;
          break;
        case Util.AUDIT: //审批
        case Util.APPEND: //审批
        case Util.SYNCHANDLE: //审批
        case Util.MODIFY: //审批
          detailDataUrl = openApi.getTaskInfo;
          submiturl = openApi.completeAudit;
          break;
        case Util.SYNCAUDIT: //并行审批
          detailDataUrl = openApi.getTaskInfo;
          submiturl = openApi.completeSyncAduit;
          break;
        case Util.CAUDIT: //自定义流程审批
          detailDataUrl = openApi.getTaskInfo;
          submiturl = openApi.completeCaduitObject;
          break;
        default:
      }
      // 查看附件
      $scope.goDetail = function (fileData) {
        if (Util.isImg(fileData)) {
          FileService.showfile(fileData);
        } else if (Util.isFile(fileData)) {
          OpenFile.open(fileData);
        } else {
          Util.Toast("移动端不支持预览");
        }
      };
      $scope.hrefLink = function (href) {
        // ios & android are opened in the different way
        OpenFile.showfile(href);
      };
      // 新增抄送功能
      $scope.copyto = function () {
        Util.setRelMoNum(1);
        localStorageService.set("chooseUsrs", $scope.aduitUsers);
        localStorageService.set("aduitAction", aduitAction);
        $location.path("chooseuser/index/getFollows");
        $rootScope.$emit("closemasker");
      };
      $scope.auditText = "";
      $scope.check = function (item) {
        if (item.name === "不同意") {
          $scope.hidedisagree = !$scope.hidedisagree;
        } else {
          $scope.showAuditContent(item);
        }
      };

      $scope.checkmore = function () {
        $scope.hidedisagree = !$scope.hidedisagree;
      };

      $scope.showAuditContent = function (item, $event) {
        if (item.id === 20) {
          // 如果是转交则执行这里的操作
          Util.showFlowTransfer({
            scope: $scope,
          });
          $rootScope.$emit("showmasker");
          return;
        }
        if (item.id === 30) {
          // 回退的操作
          Util.showFallbackFlow({
            scope: $scope,
          });
          $rootScope.$emit("showmasker");
          return;
        }
        $scope.hidedisagree = true;
        $event && $event.stopPropagation(); // 设置蛇皮操作，当页面跳转时候保存起来
        aduitAction = item;
        $rootScope.btnTitle = item.name; //不显示默认值
        $scope.operationId = item.id;
        // 显示审批内容
        Util.showAuditContent({
          scope: $scope,
        });
        $scope.showDailog = true;
        $rootScope.$emit("showmasker");
      };
      $scope.$on("closemasker", function () {
        $scope.cancel();
        $rootScope.contenttemplate.remove();
      });
      $scope.confirm = function () {
        if ($rootScope.btnTitle === "驳回" && !$scope.auditText) {
            Util.Toast("请填写驳回原因");
            return;
        }
        $scope.showDailog = false;
        $rootScope.$emit("closemasker");
        completeAudit();
      };
      $scope.cancel = function () {
        console.log("cancel");
        $scope.showDailog = false;
        $rootScope.$emit("closemasker");
        $rootScope.contenttemplate.remove();
      };

      var completeAudit = function () {
        var param = {};
        param.taskId = $scope.TaskReview.result.taskId;
        param.operationId = $scope.operationId;
        param.attachmentDesc = $scope.auditText;
        if ($rootScope.btnTitle === "不同意") {
          param.notPass = 1;
        }
        if ($rootScope.btnTitle === "已阅") {
          param.read = 1;
        }
        // 抄送人
        if ($scope.aduitUsers) {
          param.userIds = $scope.aduitUsers.map(function (item) {
            return item.id;
          });
        }
        // 审批附件
        param.attachmentName = $scope.attachmentName || "";
        // 快递单号
        if ($scope.expressNo) {
          param.expressNo = $scope.expressNo;
        }
        param.url = submiturl;
        new submitAudit(param);
      };
      // 获取回显的详细信息
      var getDetailData = function (callback) {
        var options = {};
        options.param = {};
        options.param.processInstanceId = id;
        options.param.taskId = id;
        options.url = detailDataUrl;
        options.callback = callback;
        var TaskReview = new TaskReviewService(options);
        $scope.TaskReview = TaskReview;
      };
      // 该流程无需审批，设置查看回执
      var setDetailSeen = function (processInstanceId) {
        var param = {};
        param.noloader = true;
        param.processInstanceId = processInstanceId;
        DataService.getData(openApi.publicDoc, param).then(function (data) {});
      };
      var settaskRemind = function (taskData) {
        var Userdata,
          checked = false;
        Userdata = NativeUtil.getUserInfo();
        checked =
          taskData.processStatus == "审批中" &&
          Userdata &&
          Userdata.userId == taskData.userId;
        if (checked) {
          Util.showRemindBtn({
            scope: $scope,
          });
        }
        return ($scope.showTaskRemind = checked);
      };
      // 如果是审批中并且当前登录用户是当前发起人，可以显示提醒一下
      var init = function () {
        $rootScope.nofooter = false;
        switch (TVtype) {
          case Util.CHECK:
          case Util.VIEW:
            getDetailData(function (data, formKey) {
              // 设置formKey用来进入关联模块
              $scope.formKey = formKey;
              $scope.processStatus = data.processStatus;
              // 设置提醒审批
              settaskRemind(data);
              // 显示关联模块
              $timeout(function () {
                Util.insertRelList({
                  scope: $scope,
                });
              }, 500);
            });
            break;
          case Util.READ: //通知发文无需显示审批记录
            getDetailData(function (data, formKey) {
              // 设置formKey用来进入关联模块
              $scope.formKey = formKey;
              // 通知发文自动审批
              if (formKey == "Chief_PublicDoc") {
                setDetailSeen(data.processInstanceId);
              }
              $timeout(function () {
                Util.insertAduitUsers({
                  scope: $scope,
                });
              }, 500);
            });
            $rootScope.nofooter = true;
            break;
          case Util.AUDIT:
          case Util.SYNCHANDLE: //审批
          case Util.APPEND: //审批
          case Util.SYNCAUDIT:
          case Util.CAUDIT:
          case Util.MODIFY:
            $scope.showaudit = true;
            $scope.hidedisagree = true;
            getDetailData(function (data, formKey) {
              $scope.formKey = formKey;
              // 根据返回数据显示按钮
              Util.showAudit({
                scope: $scope,
              });
              var aduitAction = localStorageService.get("aduitAction");
              if (aduitAction) {
                // 用延时，是为了在原来页面被清除之后才初始化渲染
                $timeout(function () {
                  // 审批人
                  $scope.aduitUsers = localStorageService.get("chooseUsrs");
                  if ($scope.aduitUsers) {
                    $scope.aduitUserNames = $scope.aduitUsers
                      .map(function (item) {
                        return item.name;
                      })
                      .join(",");
                  }
                  localStorageService.remove("aduitAction");
                  localStorageService.remove("chooseUsrs");
                  $scope.check(aduitAction);
                }, 200);
              }
              $timeout(function () {
                Util.insertAduitUsers({
                  scope: $scope,
                });
                Util.insertRemarkList({
                  scope: $scope,
                });
              }, 500);
            });
            break;
        }
      };
      // 初始化
      init();
    }
  );
